package restaurant;

/**
 * IOsystem class. Designed to read CSV lined text from file
 * and return this to the appropriate class for use.
 * 
 * Writing to report to file is also carried out by this class.
 * 
 * @author CT267, SG350, MSM41.
 * 
 */
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

import javax.swing.JOptionPane;

public class IOsystem {

	/**
	 * Field variables.
	 */
	private TreeSet<MenuItem> menuTreeSet = null;
	private ArrayList<OrderItem> orderList = null;

	/**
	 * Constructor creates new instance HashSet of type ArrayList.
	 **/
	public IOsystem() {
		menuTreeSet = new TreeSet<MenuItem>();
		orderList = new ArrayList<OrderItem>();
	}

	/**
	 * Save complete report file
	 * 
	 * @param value
	 */
	public void saveFile(String value, String file) {
		try {
			// Save to file.
			BufferedWriter out = new BufferedWriter(new FileWriter(file, false));
			out.write(value);
			out.close();
			System.out.println("File Saved Successfully!");
		}
		// Exit save operation.
		catch (FileNotFoundException fnf) {
			System.out.println("report.txt" + " not found ");
			System.exit(0);
		}
		// Stack trace error.
		catch (IOException ioe) {
			ioe.printStackTrace();
			System.exit(1);
		}
	}

	/**
	 * TreeSet is used to read details from input file to avoid duplication of
	 * menu items and display them in alphabetical order.
	 * 
	 * @param file
	 */
	public TreeSet<MenuItem> readMenuFile(String file) {
		Scanner scanner = null;
		try {
			scanner = new Scanner(new File(file));

			// Loop over each line in text file.
			while (scanner.hasNextLine()) {
				String input = scanner.nextLine();

				// Split line by commas.
				String[] r = input.split(",");

				// Parse line segments into variables.
				String title = r[0].trim();
				Double cost = Double.parseDouble((r[1].trim()));
				String category = r[2].trim();

				// Create and Add new menu item to treeSet object.
				try {
					menuTreeSet.add(new MenuItem(title, cost, category));
				} catch (CategoryDoseNotExist c) {
					JOptionPane.showMessageDialog(null, "CategoryDoseNotExist",
							"Oops!", JOptionPane.WARNING_MESSAGE);
					System.exit(0);
				}
			}

			scanner.close();

		} catch (FileNotFoundException fnf) {// not needed
			JOptionPane.showMessageDialog(null, "File Not Found", "TITLE",
					JOptionPane.WARNING_MESSAGE);

		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, "Number Formating Error",
					"TITLE", JOptionPane.WARNING_MESSAGE);

		} catch (ArrayIndexOutOfBoundsException aid) {
			JOptionPane.showMessageDialog(null, "Arraylist out of pound",
					"TITLE", JOptionPane.WARNING_MESSAGE);

		} catch (IllegalArgumentException ex) {
			System.err.print("dish and category may be empty");
			System.exit(0);
		}
		return menuTreeSet;
	}

	/**
	 * Load, parse, and return the menu_input.txt file.
	 * 
	 * @return orderList
	 */
	public ArrayList<OrderItem> readOrderFile(String file) {
		Scanner scanner = null;
		try {
			// Read file.
			scanner = new Scanner(new File(file));
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "File Not Found", "TITLE",
					JOptionPane.WARNING_MESSAGE);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, "Number Formating Error ",
					"TITLE", JOptionPane.WARNING_MESSAGE);
		} catch (ArrayIndexOutOfBoundsException air) {
			String error = "Not enough items in  : '" + scanner
					+ "' index position : " + air.getMessage();
			JOptionPane.showMessageDialog(null, error, "TITLE",
					JOptionPane.WARNING_MESSAGE);

		}
		// Loop over each line in text file.
		while (scanner.hasNextLine()) {
			String input = scanner.nextLine();

			// Split line by commas.
			String[] r = input.split(",");

			// Parse line segments into variables.
			// checks if tablenumber and frequency have text instead of integers
			try {
				int tableNumber = Integer.parseInt(r[0].trim());
				int frequency = Integer.parseInt(r[2].trim());
				String category = r[1].trim();
				orderList.add(new OrderItem(tableNumber, category, frequency));
			} catch (NumberFormatException ex) {
				System.err.print("TableNumber and Frequency Must be integers ");
				System.exit(0);
			} catch (IllegalArgumentException ex) {
				System.err.print("TableNumber and Frequency Must be integers ");
				System.exit(0);
			}

			// Create and Add new order item to orderList object.

		}
		scanner.close();
		return orderList;
	}

}
